package com.cloudkinto.utils;

import com.cloudkinto.common.utils.barutil.JBarcode;
import com.cloudkinto.common.utils.barutil.JBarcodeFactory;
import com.cloudkinto.common.utils.barutil.encode.Code128Encoder;
import com.cloudkinto.common.utils.barutil.encode.InvalidAtributeException;
import com.cloudkinto.common.utils.barutil.paint.BaseLineTextPainter;
import com.cloudkinto.common.utils.barutil.paint.EAN8TextPainter;
import com.cloudkinto.common.utils.barutil.paint.WidthCodedPainter;
import com.cloudkinto.common.utils.barutil.util.ImageUtil;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

/**
 * Author Administrator
 *
 * @Des
 * @Version
 * @Date 2021/1/5
 */
public class BarCodeUtils {

    //设置条形码高度
    private static final int BARCODE_HEIGHT = 40;
    //设置条形码默认分辨率
    private static final int BARCODE_DPI = ImageUtil.DEFAULT_DPI;
    //设置条形码字体样式
    private static final String FONT_FAMILY = "console";
    //设置条形码字体大小
    private static final int FONT_SIZE = 15;
    //设置条形码文本
    public static String TEXT = "";
    //创建jbarcode
    private static JBarcode jbc = null;

    public static BarCodeUtils getJBarcode() throws InvalidAtributeException {
        /**
         * 参考设置样式：
         *barcode.setEncoder(Code128Encoder.getInstance()); //设置编码
         *barcode.setPainter(WidthCodedPainter.getInstance());// 设置Painter
         *barcode.setTextPainter(BaseLineTextPainter.getInstance()); //设置TextPainter
         *barcode.setBarHeight(17); //设置高度
         *barcode.setWideRatio(Double.valueOf(30).doubleValue());// 设置宽度比率
         *barcode.setXDimension(Double.valueOf(2).doubleValue()); // 设置尺寸，大小 密集程度
         *barcode.setShowText(true); //是否显示文本
         *barcode.setCheckDigit(true); //是否检查数字
         *barcode.setShowCheckDigit(false); //是否显示检查数字
         */
        if (jbc == null) {
            //生成code128
            jbc = JBarcodeFactory.getInstance().createCode128();
            jbc.setEncoder(Code128Encoder.getInstance());
//            jbc.setTextPainter(CustomTextPainter.getInstance());
            jbc.setBarHeight(BARCODE_HEIGHT);
            jbc.setXDimension(Double.valueOf(0.8).doubleValue());
            jbc.setShowText(true);
        }
        return new BarCodeUtils();
    }

    public static String createCodeImage(String str, String path, String fileName) {
        try {
            JBarcode localJBarcode = new JBarcode(Code128Encoder.getInstance(), WidthCodedPainter.getInstance(), BaseLineTextPainter.getInstance());
            localJBarcode.setXDimension(Double.valueOf(0.6).doubleValue());
            BufferedImage localBufferedImage = localJBarcode.createBarcode(str);
            return saveToJPEG(localBufferedImage, path, fileName);
        } catch (Exception localException) {
            localException.printStackTrace();
            return null;
        }
    }

    public static byte[] createCodeImage(String str) {
        try {
            JBarcode localJBarcode = new JBarcode(Code128Encoder.getInstance(), WidthCodedPainter.getInstance(), EAN8TextPainter.getInstance());
            BufferedImage localBufferedImage = localJBarcode.createBarcode(str);

            return localBufferedImage.toString().getBytes();
        } catch (Exception localException) {
            localException.printStackTrace();
            return null;
        }
    }

    static String saveToJPEG(BufferedImage paramBufferedImage, String path, String fileName) {
        return saveToFile(paramBufferedImage, path, fileName);

    }

    static String saveToFile(BufferedImage paramBufferedImage, String path, String fileName) {
        String uri = path + File.separator + fileName;
        try {
            FileOutputStream localFileOutputStream = new FileOutputStream(uri);
            ImageUtil.encodeAndWrite(paramBufferedImage, "png", localFileOutputStream, 350, 150);
            localFileOutputStream.close();
            return uri;
        } catch (Exception localException) {
            localException.printStackTrace();
            return null;
        }
    }

}
